package com.zheng.controller;


import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPStore;
import com.zheng.common.Result;
import com.zheng.config.HsMailConfig;
import com.zheng.entity.MailDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.ofdrw.converter.ConvertHelper;
import org.ofdrw.converter.GeneralConvertException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailAuthenticationException;
import org.springframework.mail.MailSendException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.search.*;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.net.URLEncoder;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: ztl
 * @date: 2025/07/31 22:29
 * @desc:
 */

@RestController
@RequestMapping("/hsemail")
@Slf4j
public class HsEmailController {

    @Autowired
    private JavaMailSender javaMailSender;

    //发件人邮箱
    @Value("${spring.mail.username}")
    private String from;
    @Value("${spring.mail.password}")
    private String password;

    @Autowired
    private HsMailConfig mailConfig;


    /**
     * 不带附件发送邮件
     * addressee 收件人邮箱
     * subject 邮件标题
     * content 邮件内容
     *
     * @return ResultVO
     */
    @PostMapping("/send")
    public Result sendEmail(@RequestBody MailDto mail) throws AddressException {
        //判断文件格式是否正确
        //获取收件人邮箱
        String addressee = mail.getAddressee();
        //生成随机6位数字
        StringBuilder sb = (StringBuilder) imgCode().getData();

        mail.setSubject(mailConfig.getSubject());
        mail.setContent(mailConfig.getContentPrefix() + sb + mailConfig.getContentSuffix());

        SimpleMailMessage mailMessage = new SimpleMailMessage();
        // 发件人邮箱,与配置文件中保持一致,所以直接从配置文件绑定过来了
//        mailMessage.setFrom(String.valueOf(new InternetAddress("大浪8" + "<" + from + ">")));
        mailMessage.setFrom(from);
        // 收件人
        mailMessage.setTo(mail.getAddressee());
        // 标题
        mailMessage.setSubject(mail.getSubject());
        // 内容, 第一个参数为邮箱内容, 第二个参数为是否启用html格式,
        // 如果开启, 那么第一个参数如果有html标签, 会自动识别, 邮件样式更好看
        mailMessage.setText(mail.getContent());
        try {
            javaMailSender.send(mailMessage);
            return Result.success(sb.toString()); // 返回验证码
        } catch (MailAuthenticationException e) {
            return Result.error("530", "认证失败，请检查授权码和SSL配置");
        } catch (MailSendException e) {
            return Result.error("531", "邮件发送失败，请检查网络和配置");
        }
        //返回验证码
//        return Result.success();
    }

    @GetMapping("/getImgCode")
    public Result imgCode() {
        //生成随机6位数字
        SecureRandom secureRandom = new SecureRandom();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            sb.append(secureRandom.nextInt(10));
        }
        return Result.success(sb);
    }

    /**
     * 新增：接收最新的N封邮件
     *
     * @param count 要获取的邮件数量（默认10封）
     * @return 邮件列表（包含发件人、主题、内容）
     */
    @GetMapping("/receive")
    public Result receiveEmails(@RequestParam(defaultValue = "4") int count) {
        try {
            // 1. 配置IMAP服务器
            Properties props = new Properties();
            props.setProperty("mail.store.protocol", "imap");
            props.setProperty("mail.imap.host", "imap.exmail.qq.com");
            props.setProperty("mail.imap.port", "993");
            props.setProperty("mail.imap.ssl.enable", "true");
            props.setProperty("mail.imap.auth", "true");


            //这部分就是解决异常的关键所在，设置IAMP ID信息
//            HashMap IAM = new HashMap();
//            //带上IMAP ID信息，由key和value组成，例如name，version，vendor，support-email等。
//            // 这个value的值随便写就行
//            IAM.put("name","myname");
//            IAM.put("version","1.0.0");
//            IAM.put("vendor","myclient");
//            IAM.put("support-email","testmail@test.com");


            // 2. 创建Session并连接邮件服务器
            Session session = Session.getInstance(props);
            Store store = session.getStore("imap");
//            IMAPStore store = (IMAPStore) session.getStore("imap");
//            store.connect("imap.163.com", from, "LHvwWArkg8ptRTaw"); // 使用授权码登录
            store.connect(from, password); // 使用授权码登录
//            store.id(IAM);

            // 3. 打开收件箱
            Folder inbox = store.getFolder("INBOX");
            inbox.open(Folder.READ_WRITE); // 只读模式

            // 4. 获取最新的N封邮件
            Message[] messages = inbox.getMessages();
            int totalMessages = Math.min(count, messages.length);

            List<Map<String, String>> emailList = new ArrayList<>();
            for (int i = messages.length - 1; i >= messages.length - totalMessages; i--) {
                Message message = messages[i];
                Map<String, String> emailInfo = new HashMap<>();

                emailInfo.put("from", InternetAddress.toString(message.getFrom()));
                emailInfo.put("subject", message.getSubject());
                emailInfo.put("content", message.getContent().toString());
                emailInfo.put("date", message.getSentDate().toString());

                emailList.add(emailInfo);
            }

            // 5. 关闭连接
            inbox.close(false);
            store.close();

            return Result.success(emailList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("500", "接收邮件失败: " + e.getMessage());
        }
    }


    /**
     * 新增：获取最新邮件中的 PDF 附件
     *
     * @param count 检查的最新邮件数量（默认5封）
     * @return 包含 PDF 附件的 Base64 编码和文件名
     */

    @GetMapping("/pdfAttachments")
    public Result getPdfAttachments(@RequestParam(defaultValue = "5") int count) {
        try {
            // 1. 配置IMAP服务器
            Properties props = new Properties();
            props.setProperty("mail.store.protocol", "imap");
            props.setProperty("mail.imap.host", "imap.163.com");
            props.setProperty("mail.imap.port", "993");
            props.setProperty("mail.imap.ssl.enable", "true");
            props.setProperty("mail.imap.auth", "true");


            //这部分就是解决异常的关键所在，设置IAMP ID信息
            HashMap IAM = new HashMap();
            //带上IMAP ID信息，由key和value组成，例如name，version，vendor，support-email等。
            // 这个value的值随便写就行
            IAM.put("name", "myname");
            IAM.put("version", "1.0.0");
            IAM.put("vendor", "myclient");
            IAM.put("support-email", "testmail@test.com");


            // 2. 创建Session并连接邮件服务器
            Session session = Session.getInstance(props);
//            Store store = session.getStore("imap");
            IMAPStore store = (IMAPStore) session.getStore("imap");
//            store.connect("imap.163.com", from, "LHvwWArkg8ptRTaw"); // 使用授权码登录
            store.connect("zhengtianliang156@163.com", "LHvwWArkg8ptRTaw"); // 使用授权码登录
            store.id(IAM);

            // 3. 打开收件箱
            Folder inbox = store.getFolder("INBOX");
            inbox.open(Folder.READ_ONLY);

            // 4. 获取最新的N封邮件
            Message[] messages = inbox.getMessages();
            int totalMessages = Math.min(count, messages.length);

            List<Map<String, String>> pdfAttachments = new ArrayList<>();

            // 5. 遍历邮件，查找PDF附件
            for (int i = messages.length - 1; i >= messages.length - totalMessages; i--) {
                Message message = messages[i];
                if (message.isMimeType("multipart/*")) {
                    Multipart multipart = (Multipart) message.getContent();
                    for (int j = 0; j < multipart.getCount(); j++) {
                        BodyPart bodyPart = multipart.getBodyPart(j);
                        if (Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) {
                            String fileName = bodyPart.getFileName();
                            if (fileName != null && fileName.toLowerCase().endsWith(".pdf")) {
                                // 读取PDF附件
                                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                                bodyPart.getDataHandler().writeTo(outputStream);
                                byte[] pdfBytes = outputStream.toByteArray();

                                // 转换为Base64
                                String base64Pdf = Base64.getEncoder().encodeToString(pdfBytes);

                                Map<String, String> attachmentInfo = new HashMap<>();
                                attachmentInfo.put("fileName", fileName);
                                attachmentInfo.put("base64Content", base64Pdf);
                                pdfAttachments.add(attachmentInfo);
                            }
                        }
                    }
                }
            }

            // 6. 关闭连接
            inbox.close(false);
            store.close();

            return Result.success(pdfAttachments);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("500", "获取PDF附件失败: " + e.getMessage());
        }
    }


    /**
     * 分页+ 只要未读的邮件
     *
     * @param count
     * @return
     */
    @GetMapping("/receivePage")
    public Result receiveUnreadEmailsByPage(@RequestParam(defaultValue = "1") int page) {
        Store store = null;
        Folder inbox = null;
        try {
            // 1. 配置IMAP服务器（163邮箱使用IMAP协议）
            Properties props = new Properties();
            props.setProperty("mail.store.protocol", "imap");
            props.setProperty("mail.imap.host", "imap.exmail.qq.com");
            props.setProperty("mail.imap.port", "993");
            props.setProperty("mail.imap.ssl.enable", "true");
            props.setProperty("mail.imap.auth", "true");


            // 2. 创建Session并连接邮件服务器
            Session session = Session.getInstance(props);
            store = session.getStore("imap");
            store.connect(from, password);

            // 3. 打开收件箱（读写模式，才能修改邮件状态）
            inbox = store.getFolder("INBOX");
            inbox.open(Folder.READ_WRITE);

            // 4. 筛选未读邮件（Flags.Flag.SEEN = false）
            Flags seenFlag = new Flags(Flags.Flag.SEEN);
            FlagTerm unseenFlagTerm = new FlagTerm(seenFlag, false);
            Message[] unreadMessages = inbox.search(unseenFlagTerm);

            // 5. 分页处理（每页2条）
            int pageSize = 20;
            int totalUnread = unreadMessages.length;
            int totalPages = (int) Math.ceil((double) totalUnread / pageSize);

            // 页码校验
            if (page < 1 || (page > totalPages && totalPages > 0)) {
                return Result.error("400", "页码超出范围");
            }

            // 6. 获取当前页数据
            List<Map<String, String>> emailList = new ArrayList<>();
            int startIdx = (page - 1) * pageSize;
            int endIdx = Math.min(startIdx + pageSize, totalUnread);

            for (int i = startIdx; i < endIdx; i++) {
                Message message = unreadMessages[i];
                Map<String, String> emailInfo = new HashMap<>();

                emailInfo.put("from", InternetAddress.toString(message.getFrom()));
                emailInfo.put("subject", message.getSubject());
                emailInfo.put("content", getTextFromMessage(message)); // 改进的内容获取方法
                emailInfo.put("date", message.getSentDate().toString());

                // 标记为已读
                message.setFlag(Flags.Flag.SEEN, true);
                emailList.add(emailInfo);
            }

            // 7. 返回分页结果
            Map<String, Object> result = new HashMap<>();
            result.put("emails", emailList);
            result.put("currentPage", page);
            result.put("pageSize", pageSize);
            result.put("totalUnread", totalUnread);
            result.put("totalPages", totalPages);

            // 8. 关闭连接（保存更改）
            inbox.close(true);
            store.close();

            return Result.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                if (inbox != null && inbox.isOpen()) inbox.close(false);
                if (store != null && store.isConnected()) store.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return Result.error("500", "接收邮件失败: " + e.getMessage());
        }
    }

    @GetMapping("/receivePage1")
    public Result receiveUnreadEmailsByPage(@RequestParam(defaultValue = "1") int page,
                                            HttpServletResponse response) {
        Store store = null;
        Folder inbox = null;
        try {
            // 1. 配置IMAP服务器
            Properties props = new Properties();
            props.setProperty("mail.store.protocol", "imap");
            props.setProperty("mail.imap.host", "imap.exmail.qq.com");
            props.setProperty("mail.imap.port", "993");
            props.setProperty("mail.imap.ssl.enable", "true");
            props.setProperty("mail.imap.auth", "true");

            // 2. 创建Session并连接邮件服务器
            Session session = Session.getInstance(props);
            store = session.getStore("imap");
            store.connect(from, password);

            // 3. 打开收件箱
            inbox = store.getFolder("INBOX");
            inbox.open(Folder.READ_WRITE);
            List<EmailVo> emailVos = new ArrayList<>();
            // 4. 筛选未读邮件
            Flags seenFlag = new Flags(Flags.Flag.SEEN);
            FlagTerm unseenFlagTerm = new FlagTerm(seenFlag, false);
            Message[] unreadMessages = inbox.search(unseenFlagTerm);

            // 5. 分页处理
            int pageSize = 4;
            int totalUnread = unreadMessages.length;
            int totalPages = (int) Math.ceil((double) totalUnread / pageSize);


            // 6. 处理邮件和附件
            List<Map<String, Object>> emailList = new ArrayList<>();
            int startIdx = (page - 1) * pageSize;
            int endIdx = Math.min(startIdx + pageSize, totalUnread);
            IMAPFolder folderInbox = (IMAPFolder) store.getFolder("INBOX");
            for (int i = startIdx; i < endIdx; i++) {
                Message message = unreadMessages[i];

                if (Objects.isNull(message)) {
                    continue;
                }
                EmailVo emailVo = parseEmail(message);
                if (Objects.isNull(emailVo) ||
                        (Objects.isNull(emailVo.getOfdBytes()) &&
                                Objects.isNull(emailVo.getPdfBytes()))) {
                    continue;
                }
                emailVo.setMsgId(folderInbox.getUID(message));
                emailVos.add(emailVo);
                System.out.println("receiveEmail :{}" + emailVo);
            }

            // 7. 返回结果
            Map<String, Object> result = new HashMap<>();
            result.put("emails", emailList);
            result.put("currentPage", page);
            result.put("pageSize", pageSize);
            result.put("totalUnread", totalUnread);
            result.put("totalPages", totalPages);

            return Result.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("500", "接收邮件失败: " + e.getMessage());
        } finally {
            try {
                if (inbox != null && inbox.isOpen()) inbox.close(true);
                if (store != null && store.isConnected()) store.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static class EmailVo implements Serializable {
        private static final long serialVersionUID = 5382849736692084642L;
        private String userName;
        private String orderNumber;
        private String voucherNo;
        private byte[] ofdBytes;
        private byte[] pdfBytes;
        private Long msgId;

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }

        public String getOrderNumber() {
            return orderNumber;
        }

        public void setOrderNumber(String orderNumber) {
            this.orderNumber = orderNumber;
        }

        public byte[] getOfdBytes() {
            return ofdBytes;
        }

        public void setOfdBytes(byte[] ofdBytes) {
            this.ofdBytes = ofdBytes;
        }

        public byte[] getPdfBytes() {
            return pdfBytes;
        }

        public void setPdfBytes(byte[] pdfBytes) {
            this.pdfBytes = pdfBytes;
        }

        public String getVoucherNo() {
            return voucherNo;
        }

        public void setVoucherNo(String voucherNo) {
            this.voucherNo = voucherNo;
        }

        public Long getMsgId() {
            return msgId;
        }

        public void setMsgId(Long msgId) {
            this.msgId = msgId;
        }

        @Override
        public String toString() {
            return "{\"EmailVo\":{"
                    + "\"userName\":\"" + userName + "\""
                    + ", \"orderNumber\":\"" + orderNumber + "\""
                    + ", \"voucherNo\":\"" + voucherNo + "\""
                    + "}}";
        }
    }

    public static EmailVo parseEmail(Message msg) throws MessagingException, IOException {
        Address[] fromAddress = msg.getFrom();
        if (Objects.isNull(fromAddress) || fromAddress.length == 0) {
            return null;
        }
        String from = fromAddress[0].toString();
        String subject = msg.getSubject();
        Object content = msg.getContent();
        if (!(content instanceof Multipart)) {
            return null;
        }
        Multipart multipart = (Multipart) content;
        EmailVo emailVo = new EmailVo();
        for (int i = 0; i < multipart.getCount(); i++) {
            BodyPart bodyPart = multipart.getBodyPart(i);
            String disposition = bodyPart.getDisposition();
            if (BodyPart.ATTACHMENT.equalsIgnoreCase(disposition) ||
                    BodyPart.INLINE.equalsIgnoreCase(disposition)) {
                //处理附件
                fillFiles(emailVo, bodyPart);
            } else {
                // 处理内容
                fillVo(emailVo, bodyPart);
            }
        }
        return emailVo;
    }

    public static void fillFiles(EmailVo emailVo, BodyPart bodyPart) throws MessagingException, IOException {
        String fileName = bodyPart.getFileName();
        if (!StringUtils.isEmpty(fileName)) {
            String[] fileNames = fileName.split("\\.");
            if (fileNames.length != 2) {
                return;
            }
            emailVo.setVoucherNo(fileNames[0]);
            if (fileNames[1].contains("ofd")) {
                emailVo.setOfdBytes(IOUtils.toByteArray(bodyPart.getInputStream()));
            } else if (fileNames[1].contains("pdf")) {
                emailVo.setPdfBytes(IOUtils.toByteArray(bodyPart.getInputStream()));
            }
        }
    }

    /**
     * 补足基本信息
     *
     * @param emailVo
     * @param bodyPart
     * @throws MessagingException
     * @throws IOException
     */
    public static void fillVo(EmailVo emailVo, BodyPart bodyPart) throws MessagingException, IOException {
        Multipart multipart = (Multipart) bodyPart.getContent();
        for (int j = 0; j < multipart.getCount(); j++) {
            BodyPart bodyPart1 = multipart.getBodyPart(j);
            String email = bodyPart1.getContent().toString();
            emailVo.setUserName(parseUserName(email));
            emailVo.setOrderNumber(parseOrderNumber(email));
        }
    }

    private static final Pattern REGEX_PATTERN_USER_NAME =
            Pattern.compile("尊敬的 <span style=\"color: #ff764c;\">(.*?)：</span>",
                    Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
    private static final Pattern REGEX_PATTERN_ORDER_NUMBER =
            Pattern.compile("您的订单<span style=\"color: #ff764c; font-size: 14px;\">(.*?)</span>的电子发票已开具",
                    Pattern.CASE_INSENSITIVE | Pattern.DOTALL);

    public static String parseUserName(String content) {
        Matcher matcher = REGEX_PATTERN_USER_NAME.matcher(content);
        while (matcher.find()) {
            return matcher.group(1);
        }
        return "";
    }

    public static String parseOrderNumber(String content) {
        Matcher matcher = REGEX_PATTERN_ORDER_NUMBER.matcher(content);
        while (matcher.find()) {
            return matcher.group(1);
        }
        return "";
    }


    private List<Map<String, String>> processAttachments(Message message, HttpServletResponse response)
            throws Exception {
        List<Map<String, String>> attachments = new ArrayList<>();

        if (message.isMimeType("multipart/*")) {
            Multipart multipart = (Multipart) message.getContent();

            for (int i = 0; i < multipart.getCount(); i++) {
                BodyPart bodyPart = multipart.getBodyPart(i);

                if (Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) {
                    String fileName = bodyPart.getFileName();
                    String fileType = bodyPart.getContentType();
                    long fileSize = bodyPart.getSize();

                    // 附件信息
                    Map<String, String> attachmentInfo = new HashMap<>();
                    attachmentInfo.put("fileName", fileName);
                    attachmentInfo.put("fileType", fileType);
                    attachmentInfo.put("fileSize", String.valueOf(fileSize));
                    attachmentInfo.put("downloadUrl", "/downloadAttachment?fileName=" +
                            URLEncoder.encode(fileName, "UTF-8"));

                    attachments.add(attachmentInfo);

                    // 保存附件到临时目录
                }
            }
        }
        return attachments;
    }

    private String getTextFromMessage(Message message) throws Exception {
        if (message.isMimeType("text/plain")) {
            return message.getContent().toString();
        } else if (message.isMimeType("multipart/*")) {
            Multipart multipart = (Multipart) message.getContent();
            for (int i = 0; i < multipart.getCount(); i++) {
                BodyPart bodyPart = multipart.getBodyPart(i);
                if (bodyPart.isMimeType("text/plain")) {
                    return bodyPart.getContent().toString();
                }
            }
        }
        return "无法解析的邮件内容";
    }


    /**
     * 接收-时间范围内的邮件
     *
     * @param count 要获取的邮件数量（默认10封）
     * @return 邮件列表（包含发件人、主题、内容）
     */
    @GetMapping("/query4TimeRange")
    public Result query4TimeRange(@RequestParam(defaultValue = "4") int count) {
        try {
            // 1. 配置IMAP服务器
            Properties props = new Properties();
            props.setProperty("mail.store.protocol", "imap");
            props.setProperty("mail.imap.host", "imap.exmail.qq.com");
            props.setProperty("mail.imap.port", "993");
            props.setProperty("mail.imap.ssl.enable", "true");
            props.setProperty("mail.imap.auth", "true");

            // 2. 创建Session并连接邮件服务器
            Session session = Session.getInstance(props);
            Store store = session.getStore("imap");
            store.connect(from, password); // 使用授权码登录
            // 3. 打开收件箱
            Folder inbox = store.getFolder("INBOX");
            inbox.open(Folder.READ_WRITE); // 只读模式

//            Calendar startDate = Calendar.getInstance(TimeZone.getTimeZone("GMT+8")); // 使用CST时区（中国标准时间）
//            startDate.set(2025, Calendar.AUGUST, 15, 22, 0, 0);
//            startDate.set(Calendar.MILLISECOND, 0);
//
//            Calendar endDate = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
//            endDate.set(2025, Calendar.AUGUST, 15, 24, 0, 0);
//            endDate.set(Calendar.MILLISECOND, 0);
//            SearchTerm olderThan = new ReceivedDateTerm (ComparisonTerm.LE, endDate.getTime());
//            SearchTerm newerThan = new ReceivedDateTerm (ComparisonTerm.GE, startDate.getTime());

            SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date startDate = sm.parse("2025-08-15 22:00:00");
            Date endDate = sm.parse("2025-08-15 24:00:00");
//
            SearchTerm olderThan = new ReceivedDateTerm(ComparisonTerm.LE, endDate);
            SearchTerm newerThan = new ReceivedDateTerm(ComparisonTerm.GE, startDate);
            SearchTerm andTerm = new AndTerm(olderThan, newerThan);
//
//            // 获取符合条件的邮件
            Message[] unreadMessages = inbox.search(andTerm);

            log.info("queryEmailForEleJourney###查询邮箱{}, 时间范围{}到{}, 一共有{}条邮件",
                    "zhengtianliang156@163.com",
                    startDate.getTime(),
                    endDate.getTime(),
                    unreadMessages.length);

            // 5. 分页处理
            int pageSize = 20;
            int totalUnread = unreadMessages.length;
            int totalPages = (int) Math.ceil((double) totalUnread / pageSize);


            int page = 1;
            // 6. 处理邮件和附件
            List<Map<String, Object>> emailList = new ArrayList<>();
            int startIdx = (page - 1) * pageSize;
            int endIdx = Math.min(startIdx + pageSize, totalUnread);

            for (int i = 0; i < endIdx; i++) {
                Message message = unreadMessages[i];
                Map<String, Object> emailInfo = new HashMap<>();

                // 基础信息
                emailInfo.put("from", InternetAddress.toString(message.getFrom()));
                emailInfo.put("subject", message.getSubject());
//                emailInfo.put("content", getTextFromMessage(message));
                emailInfo.put("date", message.getSentDate().toString());


                // 处理附件

                emailList.add(emailInfo);
            }

            // 7. 返回结果
            Map<String, Object> result = new HashMap<>();
            result.put("emails", emailList);
            result.put("pageSize", pageSize);
            result.put("totalUnread", totalUnread);
            result.put("totalPages", totalPages);

            return Result.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("500", "接收邮件失败: " + e.getMessage());
        }
    }


    /**
     * ofd 转换为 pdf     无签名信息
     */
    @PostMapping("/ofdConverPdf")
    public Result ofdConverPdf(@RequestBody MailDto mail) throws Exception {

        String srcOfdPath = "D:\\ofdConverPdf\\a.ofd";
        String destPdfPath = "D:\\ofdConverPdf\\b.pdf"; // 最终PDF文件

        try {
            Path ofdFile = Paths.get(srcOfdPath);
            Path pdfFile = Paths.get(destPdfPath);
            ConvertHelper.toPdf(ofdFile, pdfFile); // 一步到位

            System.out.println("OFD转换PDF成功！文件: " + destPdfPath);
            // 请替换为你自己的成功返回方法
        } catch (GeneralConvertException e) {
            System.err.println("OFD转换PDF失败: " + e.getMessage());
            e.printStackTrace();
            return null;
        }

        return null;
    }

    @PostMapping("/ofdConverPdf")
    public Result ofdConverPdf3(@RequestBody MailDto mail) throws Exception {

//        String ofdPath = "C:\\Users\\Hose\\Downloads\\a.ofd";
//        String pdfPath = "C:\\Users\\Hose\\Downloads\\b.pdf"; //
//        try (OFDExporter exporter = new PDFExporterPDFBox(ofdPath, pdfPath)) {
//            exporter.export();
//        }
        return null;


    }

    public static void main(String[] args) {
        String srcOfdPath = "D:\\ofdConverPdf\\a.ofd";
        String destPdfPath = "D:\\ofdConverPdf\\b.pdf"; // 最终PDF文件

        try {
            Path ofdFile = Paths.get(srcOfdPath);
            Path pdfFile = Paths.get(destPdfPath);
            ConvertHelper.toPdf(ofdFile, pdfFile); // 一步到位

            System.out.println("OFD转换PDF成功！文件: " + destPdfPath);
            // 请替换为你自己的成功返回方法
        } catch (GeneralConvertException e) {
            System.err.println("OFD转换PDF失败: " + e.getMessage());
            e.printStackTrace();
        }

        System.out.println(4);
    }


}
